/*
 * DumpToGraphVizWD.java Dump to GraphViz WebDeso classes, attributes.
 *
 */

package topindex;

import wikipedia.sql.*;
import wikipedia.kleinberg.*;
import wikipedia.sql.PageNamespace;

import Ontology.*;
import java.util.*;

public class DumpToGraphVizWD {
    
    
    static final String CLASS_FORM      = ",shape=box,style=filled,fillcolor=yellow";
    static final String ATTR_FORM       = ",shape=circle,style=filled,fillcolor=red";
    static final String CATEGORY_FORM   = ",shape=invtriangle,style=filled,fillcolor=grey";
    static final String ARTICLE_FORM    = ",shape=triangle,fillcolor=blue";
                                        // shape=polygon,sides=4,peripheries=3";
    
    
    /** Dump WebDeso classes to text file in GraphViz format */
    public static void dumpClasses (Map<String, Classes> class_name2object, 
                                    Hashtable htClasses, DumpToGraphViz dump) {
        //dump.Dump(articles, "Article  nodes");
        
        dump.file_dot.PrintNL("// WebDeso classes");
        for(String name:class_name2object.keySet()) {
            Classes c = class_name2object.get(name);
            
            // Create line: "cl_Safety [label=\"Safety\\nid=7\n\\nx=12.0\ny=3.0\"];\n"
            //dump.file_dot.PrintNL(new String("cl_" + name + " [label=\"" + name + "\"" +
            
            // Create line: "cl_ID [label=\"Safety\\nid=7\n\\nx=12.0\ny=3.0\"];\n"
            dump.file_dot.PrintNL(new String("cl_" + c.getClassID() + " [label=\"" + name + "\"" +
                    //\\nid=" + c.getClassID() +      // id and the name 
                    //"\\nx=" + x + "\\ny=" + y + "\"" +                        // x and y values
                    CLASS_FORM +                            // node's shape form
                    "];\n")
            );
            
            
            // Create line: "cl_ParentName -> cl_ClassName [style=bold];\n"
            Classes p = (Classes)htClasses.get(c.getParentID());
            if(null != p) {
                //dump.file_dot.PrintNL( "cl_" + p.getClassName() + " -> " + "cl_" + c.getClassName() + " [style=bold];\n");
                dump.file_dot.PrintNL( "cl_" + p.getClassID() + " -> " + "cl_" + c.getClassID() + " [style=bold];\n");
            }
        }
        dump.file_dot.Flush();
    }
    
    /** Dump WebDeso attributes to text file in GraphViz format */
    public static void dumpAttributes ( Map<String, Attributes> attr_name2object,
                                        Hashtable htAttributes,
                                        Vector      vClassAttr,
                                        DumpToGraphViz dump)
    {
        dump.file_dot.PrintNL("// WebDeso attributes");
        for(String name:attr_name2object.keySet()) {
            Attributes c = attr_name2object.get(name);
            
            // Create line: "cl_Safety [label=\"Safety\\nid=7\n\\nx=12.0\ny=3.0\"];\n"
            //dump.file_dot.PrintNL(new String("cl_" + name + " [label=\"" + name + "\"" +
            
            // Create line: "cl_ID [label=\"Safety\\nid=7\n\\nx=12.0\ny=3.0\"];\n"
            dump.file_dot.PrintNL(new String("attr_" + c.getAttrID() + " [label=\"" + name + "\"" +
                    //\\nid=" + c.getClassID() +      // id and the name 
                    //"\\nx=" + x + "\\ny=" + y + "\"" +                        // x and y values
                    ATTR_FORM +                            // node's shape form
                    "];\n")
            );
            
            /*
            
            Attributes p = (Attributes)htAttributes.get(c.());
            if(null != p) {
                //dump.file_dot.PrintNL( "cl_" + p.getClassName() + " -> " + "cl_" + c.getClassName() + " [style=bold];\n");
                dump.file_dot.PrintNL( "cl_" + p.getClassID() + " -> " + "cl_" + c.getClassID() + " [style=bold];\n");
            }*/
        }
        dump.file_dot.Flush();
        
        
        // Create line: "cl_ClassID -> attr_AttrID;\n"
        dump.file_dot.PrintNL("// WebDeso classes to attributes links");
        for(Object o:vClassAttr) {
            ClassAttr ca = (ClassAttr)o;
            dump.file_dot.PrintNL( "cl_" + ca.getClassID() + " -> " + "attr_" + ca.getAttrID() + " [color=green];\n");
        }
        dump.file_dot.Flush();
    }

    /** Dump Categories Similar to Classes and Attributes to text file in GraphViz format.
     *  attr_name2object could be null.
     */
    public static void dumpCategoriesSimilar (  Map<String, Classes>    class_name2object, 
                                                Map<String, Attributes> attr_name2object, 
                                                Map<String, CategorySim> category_sim,
                                                DumpToGraphViz dump) {
        
        dump.file_dot.PrintNL("// Wikipedia categories which are similar to WebDeso classes and attributes");
        for(String name:category_sim.keySet()) {
            CategorySim cs = category_sim.get(name);
            
            // print node (category or class) only if there are links to classes and attributes of WebDeso
            if(null != cs.sims && 0 < cs.sims.length) {
                String form = (cs.ns == PageNamespace.CATEGORY) ? CATEGORY_FORM : ARTICLE_FORM;

                // Create line: "wiki_Safety [label=\"Safety\\nid=7\n\\nx=12.0\ny=3.0\"];\n"
                dump.file_dot.PrintNL(new String("wiki_" + cs.id + " [label=\"" + name + "\"" + // id and the name 
                        form +                            // node's shape form
                        "];\n")
                );


                // Create line to class:    "wiki_categoryID -> cl_ClassID  [style=dotted];\n"
                // Create line to attribute:"wiki_categoryID -> attr_AttrID [style=dotted,color=green];\n"
                for(int i=0; i<cs.sims.length; i++) {
                    if(CategorySim.WD_CLASS == cs.sims[i].type) {
                        Classes cl = class_name2object.get(cs.sims[i].title);
                        if(null != cl) {
                            dump.file_dot.PrintNL( "wiki_" + cs.id + " -> " + "cl_" + cl.getClassID() + " [label=\"dist="+cs.sims[i].dist+"\",style=dotted];\n");
                        }
                    }
                    if(CategorySim.WD_ATTRIBUTE == cs.sims[i].type && null != attr_name2object) {
                        Attributes a = attr_name2object.get(cs.sims[i].title);
                        if(null != a) {
                            dump.file_dot.PrintNL( "wiki_" + cs.id + " -> " + "attr_" + a.getAttrID() + " [label=\"dist="+cs.sims[i].dist+"\",style=dotted,color=green];\n");
                        }
                    }
                }
            }
        }
        dump.file_dot.Flush();
         
    }

    
    
    
}
